﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace NeuroSpeech.XamlTypeResolver
{
    public class XTType {

        public XTType()
        {

        }

        public XTType(Type type)
        {
            Type = type;
            Name = type.Name;
            Namespace = type.Namespace;
        }

        public string Name { get; set; }

        public string Namespace { get; set; }

        public Type Type { get; set; }

    }

    public class XTNamespace
    {

        private XTNamespace ()
    	{

	    }

        public string XmlNamespace { get; set; }

        public string ClrNamespace { get; set; }

        public List<string> XamlClrNamespaces { get; set; }

        public string AssemblyName { get; set; }

        public string Prefix { get; set; }

        private static List<XTNamespace> GlobalList = new List<XTNamespace>();

        

        private bool Equals(Assembly asm, string xmlNamespace, string clrNamespace) {
            if (!string.IsNullOrWhiteSpace(AssemblyName)) {
                if(asm!=null)
                    return asm.GetName().Name == AssemblyName && clrNamespace == ClrNamespace;
            }
            if (!string.IsNullOrWhiteSpace(XmlNamespace))
                return xmlNamespace == XmlNamespace;
            return false;
        }

        public static XTNamespace GetNamespace(Assembly asm, string xmlNamespace, string clrNamespace, string prefix){
            XTNamespace ns = GlobalList.FirstOrDefault( x=>x.Equals(asm,xmlNamespace,clrNamespace) );
            if (ns == null) {
                ns = new XTNamespace { 
                    AssemblyName = asm==null ? "" : asm.GetName().Name,
                    ClrNamespace = clrNamespace,
                    XmlNamespace = xmlNamespace,
                    Prefix = prefix
                };
                GlobalList.Add(ns);
            }
            return ns;
        }


        #region public override string  ToString()
        public override string ToString()
        {
            if (string.IsNullOrWhiteSpace(AssemblyName))
            {
                return XmlNamespace;
            }
            return ClrNamespace + "; assembly=" + AssemblyName;
        }
        #endregion

        private List<XTType> typeList = new List<XTType>();
        public IList<XTType> TypeList {
            get {
                return typeList;
            }
        }

        public static XTNamespace[] Resolve(IEnumerable<Assembly> assemblyCollection) {
            List<XTNamespace> nsList = new List<XTNamespace>();

            foreach (Assembly item in assemblyCollection)
            {
                XTAssembly xat = new XTAssembly(item);
            }

            foreach (XTNamespace item in GlobalList)
            {
                item.Sort();
            }

            return GlobalList.ToArray();
        }

        #region private void Sort()
        private void Sort()
        {
            typeList = typeList.OrderBy(x => x.Name).ToList();
        }
        #endregion


    }

    public enum XTNamespaceType { 
        CLR,
        XAML
    }
}
